import { Point } from "./Point.js";


export class Transformation {
  _a;
  _b;
  _c;
  _d;
  constructor(a, b, c, d) {
    if (Array.isArray(a)) {
      this._a = a[0];
      this._b = a[1];
      this._c = a[2];
      this._d = a[3];
      return;
    }
    this._a = a;
    this._b = b;
    this._c = c;
    this._d = d;
  }
  transform(point, scale) {
    return this._transform(point.clone(), scale);
  }

  _transform(point, scale) {
    scale = scale || 1;
    point.x = scale * (this._a * point.x + this._b);
    point.y = scale * (this._c * point.y + this._d);
    
    return point;
  }

  untransform(point, scale) {
    scale = scale || 1;
    return new Point(
      (point.x / scale - this._b) / this._a,
      (point.y / scale - this._d) / this._c
    );
  }
}

export function toTransformation(a, b, c, d) {
  return new Transformation(a, b, c, d);
}
